﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SuperMarketSystem
{
    //These are the handler delegates for events handled by this class.
    public delegate void StartEventHandler(SelfCheckStation sender);
    public delegate void CustomerStartEventHandler(StartInterface sender);
    public delegate void RestockerStartEventHandler(StartInterface sender);
    public delegate void ManagerStartEventHandler(StartInterface sender);
    public delegate void FinishedEventHandler(Control sender);

    public partial class SelfCheckStation : Form
    {
        //Handled by SelfCheckStation
        public StartEventHandler starthandler;
        public CustomerStartEventHandler customerhandler;
        public RestockerStartEventHandler restockerhandler;
        public ManagerStartEventHandler managerhandler;
        public FinishedEventHandler finishhandler;

        //Handler passed down from AutoScoutsControl
        public CheckoutEventHandler checkouthandler;
        public NewItemEventHandler itemhandler;
        public ItemLookupEventHandler lookuphandler;
        public GetInventoryEventHandler geteventhandler;
        public TransactionEventHandler transactionhandler;
        public RestockEventHandler restockitemhandler;
        public TimerEventHandler timerhandler;

        AutoScoutsControl manager;
        event StartEventHandler startEvent;

        public SelfCheckStation(AutoScoutsControl control)
        {
            InitializeComponent();
            manager = control;

            starthandler = new StartEventHandler(onStart);
            startEvent += starthandler;

            customerhandler = new CustomerStartEventHandler(onCustomerStart);
            restockerhandler = new RestockerStartEventHandler(onRestockerStart);
            managerhandler = new ManagerStartEventHandler(onManagerStart);
            finishhandler = new FinishedEventHandler(onFinished);

            checkouthandler = manager.checkouthandler;
            itemhandler = manager.itemhandler;
            lookuphandler = manager.lookuphandler;
            geteventhandler = manager.geteventhandler;
            transactionhandler = manager.transactionhandler;
            restockitemhandler = manager.restockitemhandler;
            timerhandler = manager.timerhandler;

            if (startEvent != null)
                startEvent(this);
        }

        void onStart(SelfCheckStation sender)
        {
            sender.Controls.Add(new StartInterface(this));
        }

        public void onFinished(Control sender)
        {
            Controls.Clear();
            if (startEvent != null)
                startEvent(this);
        }

        void onCustomerStart(StartInterface sender)
        {
            Control parent = sender.Parent;
            parent.Controls.Remove(sender);
            parent.Controls.Add(new SelfCheckControl(this));
            sender.Dispose();
        }
        void onRestockerStart(StartInterface sender)
        {
            Control parent = sender.Parent;
            parent.Controls.Remove(sender);
            parent.Controls.Add(new RestockerControl(this));
            sender.Dispose();
        }
        void onManagerStart(StartInterface sender)
        {
            Control parent = sender.Parent;
            parent.Controls.Remove(sender);
            parent.Controls.Add(new ManagerControl(this));
            sender.Dispose();
        }
    }
}
